package com.pinda.controller;


import com.github.pagehelper.PageInfo;
import com.pinda.auth.entity.PdCoreOrg;
import com.pinda.commonutils.Page;
import com.pinda.commonutils.Result;
import com.pinda.commonutils.StatusCode;
import com.pinda.feign.PdAuthFeignClient;
import com.pinda.feign.PdOrderCargoFeignClient;
import com.pinda.feign.PdTaskTransportFeignClient;
import com.pinda.service.IPdDriverJobService;
import com.pinda.service.IPdTransportOrderService;
import com.pinda.service.IPdTransportOrderTaskService;
import com.pinda.work.DTO.TaskTransportDTO;
import com.pinda.oms.entity.PdOrderCargo;
import com.pinda.oms.vo.PdOrderCargoSerach;
import com.pinda.oms.vo.PdOrderCargoView;
import com.pinda.work.entity.PdDriverJob;
import com.pinda.work.entity.PdTaskTransport;
import com.pinda.service.IPdTaskTransportService;
import com.pinda.work.entity.PdTransportOrderTask;
import com.pinda.work.vo.pdtasktransportQueryView;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 运输任务表 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-task-transport")
public class PdTaskTransportController {
    @Resource
    IPdTaskTransportService iPdTaskTransportService;

    @Autowired
    private PdTaskTransportFeignClient pdTaskTransportFeignClient;

    @Autowired
    private IPdTransportOrderTaskService transportOrderTaskService;

    @Resource
    private IPdDriverJobService iPdDriverJobService;
    @Resource
    private PdAuthFeignClient pdAuthFeignClient;

    /**
     * 新增运输任务
     *
     * @param dto 运输任务信息
     * @return 运输任务信息
     */
    @PostMapping("")
    public TaskTransportDTO save(@RequestBody TaskTransportDTO dto) {
        PdTaskTransport transportOrder = new PdTaskTransport();
        BeanUtils.copyProperties(dto, transportOrder);
        iPdTaskTransportService.saveTaskTransport(transportOrder);
        //保存与运单的关联关系

        if (dto.getTransportOrderIds() != null && dto.getTransportOrderIds().size() > 0) {
            List<PdTransportOrderTask> transportOrderTaskList = dto.getTransportOrderIds().stream().map(transportOrderId -> {
                PdTransportOrderTask transportOrderTask = new PdTransportOrderTask();
                transportOrderTask.setTransportOrderId(transportOrderId);
                transportOrderTask.setTransportTaskId(transportOrder.getId());
                return transportOrderTask;
            }).collect(Collectors.toList());
            transportOrderTaskService.batchSaveTransportOrder(transportOrderTaskList);
        }
        TaskTransportDTO result = new TaskTransportDTO();
        BeanUtils.copyProperties(transportOrder, result);
        result.setTransportOrderIds(dto.getTransportOrderIds());
        return result;
    }

    @Autowired
    private PdOrderCargoFeignClient pdOrderCargoFeignClient;

    @GetMapping("/transport/{status}/{driverId}")
    public Result<PdTaskTransport> transport(@PathVariable Integer status, @PathVariable String driverId) {
        PdDriverJob pdDriverJob = iPdDriverJobService.selectOne(status, driverId);
        if (pdDriverJob!=null){
            Result<PdTaskTransport> transport = iPdTaskTransportService.transport(pdDriverJob.getTaskTransportId());
            PdTaskTransport data = transport.getData();
            //起始机构经纬度
            PdCoreOrg pdCoreOrgStart = pdAuthFeignClient.latitudeAndLongitude(data.getStartAgencyId());
            data.setDeliveryLatitude(pdCoreOrgStart.getLatitude());
            data.setDeliveryLongitude(pdCoreOrgStart.getLongitude());
            //终点机构经纬度
            PdCoreOrg pdCoreOrgEnd = pdAuthFeignClient.latitudeAndLongitude(data.getEndAgencyId());
            data.setDeliverLatitude(pdCoreOrgEnd.getLatitude());
            data.setDeliverLongitude(pdCoreOrgEnd.getLongitude());
            transport.setData(data);
            return transport;
        }else {
            return new Result<>(false,300,"没有在途任务",null);
        }

    }

    /**
     * 分页查询运输任务管理
     */
    @PostMapping("/lcqueryviewresultController")
    public Result<PageInfo<pdtasktransportQueryView>> queryviewresultController(@RequestBody Page<pdtasktransportQueryView> page){
        return new Result<>(true, StatusCode.OK,"查询成功",this.pdTaskTransportFeignClient.querypageInfoalltasktansport(page));
    }
    /**
     * 根据订单和运单编号查询得到获取该订单对应到的货物信息表
     */
    @PostMapping("/lcselectOrderCargoaByorderid")
    public  Result<List<PdOrderCargoView>> selectOrderCargoaByorderidController(@RequestBody PdOrderCargoSerach serach){
        return new Result<>(true,StatusCode.OK,"查询成功", this.pdOrderCargoFeignClient.selectOrderCargoaByorderidController(serach));
    }



    /**
     * 修改运输任务信息
     *
     * @param id  运输任务id
     * @param dto 运输任务信息
     * @return 运输任务信息
     */
    @PutMapping("/{id}")
    public TaskTransportDTO updateById(@PathVariable(name = "id") String id, @RequestBody TaskTransportDTO dto) {
        dto.setId(id);
        PdTaskTransport taskTransport = new PdTaskTransport();
        BeanUtils.copyProperties(dto, taskTransport);
        iPdTaskTransportService.updateById(taskTransport);
        transportOrderTaskService.del(null, id);
        //保存与运单的关联关系
        if (dto.getTransportOrderIds() != null && dto.getTransportOrderIds().size() > 0) {
            List<PdTransportOrderTask> transportOrderTaskList = dto.getTransportOrderIds().stream().map(transportOrderId -> {
                PdTransportOrderTask transportOrderTask = new PdTransportOrderTask();
                transportOrderTask.setTransportOrderId(transportOrderId);
                transportOrderTask.setTransportTaskId(id);
                return transportOrderTask;
            }).collect(Collectors.toList());
            transportOrderTaskService.batchSaveTransportOrder(transportOrderTaskList);
        }
        return dto;
    }
}
